/**
 * 图纸管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图纸管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <!-- <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.deptName"
          placeholder="输入部门名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.deptNo"
          placeholder="输入部门代码"
        ></el-input>
      </el-form-item> -->
      <el-form-item>
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        > -->
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="uploadRvt()"
          >上传dwg</el-button
        >
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        > -->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <!-- <el-table-column align="center" type="selection" width="60">
      </el-table-column> -->
      <el-table-column sortable prop="dwgName" label="文件名称" min-width="300">
      </el-table-column>
      <el-table-column sortable prop="fileSize" label="文件大小" width="150">
      </el-table-column>
      <el-table-column sortable prop="status" label="文件状态" width="150">
      </el-table-column>

      <el-table-column sortable prop="updateDate" label="上传时间" width="200">
        <template slot-scope="scope">
          <div>{{ scope.row.updateDate | timestampToTime }}</div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="info"
            @click="openModel(scope.$index, scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="部门名称" prop="deptName">
          <el-input
            size="small"
            v-model="editForm.deptName"
            auto-complete="off"
            placeholder="请输入部门名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门代码" prop="deptNo">
          <el-input
            size="small"
            v-model="editForm.deptNo"
            auto-complete="off"
            placeholder="请输入部门代码"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <!-- 上传rvt界面 -->
    <el-dialog
      :title="title"
      :visible.sync="uploadRvtForm"
      width="50%"
      @click="closeDialog"
    >
      <el-form label-width="120px">
        <div style="padding-top: 10px">
          <input
            type="file"
            name="rvtFile"
            id="rvtFile"
            value=""
            placeholder="上传后请等一会儿刷新"
          />
        </div>
        <div class="progress">
          <div id="progress-div1"></div>
        </div>
        <div style="padding-top: 30px">
          <h3>上传说明</h3>
          <p>该引擎处于测试集成阶段</p>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitRvt()"
          >上传</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import { NetHandle } from "../../components/bimjs/net.js";
import { QmodelOption } from "../../components/bimjs/QmodelServer.js";
export default {
  data() {
    return {
      netHandle: null,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      uploadRvtForm: false, //控制上传模型页面是否显示和隐藏
      title: "添加",
      editForm: {
        deptId: "",
        deptName: "",
        deptNo: "",
        token: localStorage.getItem("logintoken"),
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
        ],
        deptNo: [
          { required: true, message: "请输入部门代码", trigger: "blur" },
        ],
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: "",
        varName: "",
        token: localStorage.getItem("logintoken"),
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    let _this = this;
    this.netHandle = new NetHandle(new QmodelOption());
    this.netHandle.getToken().then((a) => {
      _this.getdata(_this.formInline);
    });
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取dwg列表
    getdata(parameter) {
      debugger;
      this.loading = true;
      let _this = this;

      /***
       * demo ,简单合并请求
       */
      this.netHandle
        .getDwgs(10000)
        .then((res) => {
          console.log(res);
          _this.loading = false;
          if (res.success == false) {
            _this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            _this.listData = res.data.slice(
              (_this.formInline.page - 1) * _this.formInline.limit,
              _this.formInline.page * _this.formInline.limit
            );
            // 分页赋值
            _this.pageparm.currentPage = _this.formInline.page;
            _this.pageparm.pageSize = _this.formInline.limit;
            _this.pageparm.total = res.count;
          }
        })
        .catch((err) => {
          _this.loading = false;
          _this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    uploadRvt: function () {
      this.uploadRvtForm = true;
    },
    submitRvt: function () {
      var _this = this;
      var fff = document.getElementById("rvtFile").files[0];
      if (fff === null || fff.name === "") return;
      if (fff.name.substr(fff.name.lastIndexOf(".") + 1) !== "dwg") {
        alert("可以上传dwg文件吗？");
        return;
      }
      if (fff.size > 1024 * 1024 * 100) {
        alert("网络有限，可以上传小点的dwg吗？");
        return;
      }
      var event = function (e) {
        console.log(e);
        //loaded代表上传了多少
        //total代表总数为多少
        var progressRate = (e.loaded / e.total) * 100 + "%";

        //通过设置进度条的宽度达到效果
        document.getElementById("progress-div1").style.width = progressRate;
      };
      _this.netHandle.uploadDwgFile(fff, event).then((e) => {
        if (e == 1) {
          _this.getdata();
          alert("已上传，请耐心等待，过会儿回来看。");
          this.uploadRvtForm = false;
        } else alert("网络不通，请联系站长！");
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.editForm.deptId = row.deptId;
        this.editForm.deptName = row.deptName;
        this.editForm.deptNo = row.deptNo;
      } else {
        this.title = "添加";
        this.editForm.deptId = "";
        this.editForm.deptName = "";
        this.editForm.deptNo = "";
      }
    },
    openModel: function (rowindex, row) {
      window.location.href = window.location.origin + "/#/dwg?file=" + row.ext1;
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          deptSave(this.editForm)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.success) {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: "公司保存成功！",
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("公司保存失败，请稍后再试！");
            });
        } else {
          return false;
        }
      });
    },
    // 删除
    deleteUser(index, row) {
      let _this = this;
      _this
        .$confirm("确定要删除吗?", "信息", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          _this
            .deleteModelMgr(row.id, row.xtype)
            .then((res) => {
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "已删除!",
                });
                _this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              _this.loading = false;
              _this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          _this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.progress {
  width: 450px;
  height: 35px;
  border: 1px solid #ccc;
  border-radius: 10px;
  overflow: hidden;
}
/* 初始状态设置进度条宽度为0px */

.progress > div {
  width: 0px;
  height: 100%;
  background-color: yellowgreen;
  transition: all 0.3s ease;
}
</style>

 